package be.djdb.mdi;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import be.djdb.generators.data.SuperItems;
import be.djdb.utils.Projectresources;



public class Mywindowmenu extends JMenu {
	private Mydecorateblemdi mdi;
	private JMenuItem cascade ;
	private JMenuItem tile ;
	public Mywindowmenu(MYMDI body, SuperItems translations) {
		this.mdi = mdi;
		setText(translations.getlblFor("window"));
		cascade = new JMenuItem(translations.getlblFor("Cascade"));
		tile =  new JMenuItem(translations.getlblFor("Tile"));
		setup();
		gui();
		addMenuListener(new MenuListener() {
			public void menuCanceled(MenuEvent e) {
			}
			public void menuDeselected(MenuEvent e) {
				removeAll();
			}
			public void menuSelected(MenuEvent e) {
				build();
			}
		});
	}
	private void setup(){		
		cascade.addActionListener(
				new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Mywindowmenu.this.mdi.setFrames_cascade();
			}
		});
		tile.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
				Mywindowmenu.this.mdi.setFrames_tile();
			}
		});
	}
	private void gui(){
		add(cascade);
		add(tile);
	}
	private void build(){
		int i;
		MenuItemframe menuitem;
		JInternalFrame[] array = mdi.getFrames();
		gui();
		if (array.length > 0)
		for (i = 0; i < array.length; i++) {
			menuitem = new MenuItemframe(array[i]);
			menuitem.setState(i == 0);
			menuitem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent ae) {
					JInternalFrame frame = ((MenuItemframe) ae.getSource()).getFrame();
					frame.moveToFront();
					try {
						frame.setSelected(true);
					} catch (PropertyVetoException e) {
						e.printStackTrace();
					}
				}
			});
			menuitem.setIcon(array[i].getFrameIcon());
			add(menuitem);
		}
	}
	class MenuItemframe  extends JCheckBoxMenuItem{
		 private JInternalFrame frame;
		 public MenuItemframe(JInternalFrame frame) {
		   this.frame = frame;
		 }
		 public JInternalFrame getFrame() {
		   return frame;
		 }
	}

}
